<template>
    <div>
        <el-table :data="store.getters['role/getRole']" border style="width: 100%">
            <el-table-column prop="id" label="角色编号" width="180" />
            <el-table-column prop="rolename" label="角色名称" width="180" />
            <el-table-column label="状态">
                <template v-slot="item">
                    <div>
                        <el-button v-if="item.row.status == 1" type="primary" plain>启用</el-button>
                        <el-button v-else type="danger" plain>禁用</el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template v-slot="item">
                    <div>
                        <el-button type="primary" @click="edit(item.row.id)">编辑</el-button>
                        <el-button type="danger" @click="del(item.row.id)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script lang='ts' setup>
// 引入edit
import { defineEmits} from 'vue'
// 引入删除接口
import { roleDelete } from '@/request/api'
// 引入消息弹框
import { ElMessageBox, ElMessage } from 'element-plus'
// 引入仓库
import { useStore } from 'vuex'
let store = useStore()
// 获取vuex中的role列表
const getRole = () => {
    store.dispatch('role/getRoleAction')
}
// 页面打开调取列表
getRole()
let emit = defineEmits(['edit'])
// 编辑事件
const edit = (id: number) => {
    emit('edit', id)
}
// 删除事件
const del = (id: number) => {
    ElMessageBox.confirm(
        '你确定要删除吗?',
        '提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            // 调用删除接口
            roleDelete({ id }).then(res => {
                if (res.code == 200) {
                    ElMessage.success(res.msg)
                }
                // 重新调用列表
                getRole()
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消删除',
            })
        })
}


</script>
<style scoped></style>